﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.User
{
    public class Login : BasePage
    {
        protected string postusername = "";
        protected string username;
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;
            
            HandleCommon();

            if (IsPost) //处理登陆请求
            {
                HandleLogin();
            }
            else
            {
               username = CookiesTools.GetCookie("youhpPreUser");

                int n=RequestTools.GetUrlReferrer().IndexOf("Register");
                if ((RequestTools.GetUrlReferrer() == "") || (RequestTools.GetUrlReferrer().IndexOf("login") > -1) ||
                    RequestTools.GetUrlReferrer().IndexOf("Logout")>-1||(n > -1))
                {
                    string TempUrl = "/User/HomePage.aspx";

                    CookiesTools.SetPreUrl(this.GetUrl(TempUrl));
                }
                else
                {
                    CookiesTools.SetPreUrl(RequestTools.GetUrlReferrer());
                }
            }
        }

        protected void HandleCommon()
        {
            this.SetTitle("用户登录");
            this.AddLocation("用户登录", "/User/Login.aspx");

            if (UserData.Id > 0)
            {
                this.AddErrLine("您已经登录了，无须再次登录!");
                //TempUrl = SessionHelper.GetString("Referrer");
                //this.SetUrl(TempUrl);
                //this.SetMetaRefresh();
                return;
            }
            
        }

        private void HandleLogin()
        {
            bool loginsucc = false;
            string userName = RequestTools.GetFormString("username");
            string password = Utils.MD5(RequestTools.GetFormString("password"), 16);
            int expireTime = RequestTools.GetFormInt("expiretime",-1);

            try
            {
                UserData = UserBLL.Instance.CheckUserLogin(userName, password);
                if (UserData.Id > 0)
                {
                    //更新登录状态
                    UserBLL.Instance.UpdateUserLoginState(UserData.Id, 1);
                    //写cookie
                    //if (expireTime <= 0) expireTime = -1;
                    CookiesTools.WriteUserCookie(UserData.Id.ToString(), UserData.UserName, expireTime);
                    CookiesTools.WriteUserInfoCookie(UserData.UserName,UserData.Password,UserData.Id, 5256000);
                    //CookiesTools.WriteUserNameCookie(UserData.UserName);    //将本次登陆的用户名写入cookie，方便下次用户的输入

                    //提示登陆成功，并根据用户类型以及上一个要求登陆的页面来决定转向的url
                    this.AddMsgLine("登录成功");
                    string nextUrl = CookiesTools.GetPreUrl();
                    this.SetUrl2(nextUrl);
                    this.SetMetaRefresh(3, nextUrl);

                    loginsucc = true;
                }
            }
            catch(Exception e)
            {
                this.AddErrLine(e.ToString());
            }

            if (loginsucc == false)
            {
                this.AddErrLine("登录失败,用户名或密码错误！");
            }
        }
    }
}
